<template>
	<div class="box" style="background: #fff;padding: 10px;">
		<!-- 单元格 -->
		<ul class="list">
			<li v-for="(item, index) in userList" :key="index">
				<div class="header">
					<image :src="item.headImage" mode=""></image>
					<p>{{ item.nickname }}</p>
				</div>

				<!-- 授权状态码 -->
				<van-cell-group>
					<van-cell icon="shop-o">
						<view slot="title">
							<view class="van-cell-text">服务通知</view>
						</view>

						<div slot='right-icon'>
							<van-switch :checked="item.serviceNotice" @change="serviceNotice($event, item)"
								size="24px" />
						</div>
					</van-cell>

					<van-cell icon="shop-o">
						<view slot="title">
							<view class="van-cell-text">邮箱通知</view>
						</view>

						<div slot='right-icon'>
							<van-switch :checked="item.isSendEmailAllowed" @change="sendEmailAllowed($event, item)"
								size="24px" />
						</div>
					</van-cell>
				</van-cell-group>
			</li>
		</ul>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},

				userList: [],

				jurisdiction: ''
			};
		},

		onLoad() {
			this.userInfo = uni.getStorageSync("userInfo") || {};
			this.getUserList(); //获取用户列表
		},

		methods: {
			//获取用户列表 
			async getUserList() {
				let result = await this.$axios("overallSituation", {
					state: "look",
					databaseName: "userList",
					_id: this.userInfo._id == '66794a7555b3372a1f80e1e1' ? '' : this.userInfo._id
				});

				this.userList = result.data;
			},

			// 服务通知发生变化
			async serviceNotice(e, item) {

				uni.showLoading({
					title: '加载中'
				})

				let userObj = item;
				let authorization = {};
				let getLoginInfo = {};
				// 如果开启通知 获取信息
				if (e.detail) {
					// 调用是否授权
					authorization = await this.authorization();

					// 获取用户信息 存储 openId
					getLoginInfo = await this.$getLoginInfo();
					console.log(getLoginInfo, 'getLoginInfo====================');

					userObj.serviceNotice = authorization.code == 200 ? true : false; // 改变开关状态
					userObj.openId = getLoginInfo.openid; // 改变保存用户的openId
				} else {
					userObj.serviceNotice = false;
				}

				let result = await this.$axios("overallSituation", {
					state: "update",
					databaseName: "userList",
					...userObj,
				});

				if (result.code == 200) {
					this.getUserList();
				}

				uni.hideLoading()
			},

			// 授权消息订阅
			authorization() {
				return new Promise((resolve, reject) => {
					wx.requestSubscribeMessage({
						tmplIds: ["fEm8kKmKVUASmZCyjHmrniw06a3EOnWBGnL27bgLhb8"], // 传递模板 ID 数组
						success(res) {
							// 成功时，通常 res 是一个对象，其中包含了用户对不同模板消息的订阅状态
							resolve({
								code: 200,
								message: "授权成功",
							}); // 将成功的结果传递给 Promise 的 then 方法
						},
						fail(err) {
							// 失败时，处理错误
							reject({
								code: 500,
								message: "授权失败",
							}); // 将错误传递给 Promise 的 catch 方法
						},
					});
				});
			},

			// 邮箱提醒授权
			async sendEmailAllowed(e, item) {
				if (!item.email) {
					uni.showToast({
						title: "填写邮箱后继续",
						icon: "none",
						duration: 2000
					})
					return
				}

				item.isSendEmailAllowed = e.detail

				let result = await this.$axios("overallSituation", {
					state: "update",
					databaseName: "userList",
					...item,
				});

				if (result.code == 200) {
					this.getUserList()
				}
			},

			// 切换主题样式 
			async cutHomeStyle(e, item) {
				item.classType = e.detail
				let result = await this.$axios("overallSituation", {
					state: "update",
					databaseName: "userList",
					...item,
				});

				if (result.code == 200) {
					if (this.userInfo._id == item._id) {
						uni.setStorageSync('userInfo', item)
					}
					this.getUserList()
				}
			},

			// 权限单选框发生变化
			async radioChange(e , item) {
				item.jurisdiction = e.detail
				if(e.detail == '1'){
					item.jurisdictionName = '高权限'
				}else if(e.detail == 2) {
					item.jurisdictionName = '中权限'
				}else if(e.detail == 3){
					item.jurisdictionName = '低权限'
				}
				
				let result = await this.$axios("overallSituation", {
					state: "update",
					databaseName: "userList",
					...item,
				});
				
				if (result.code == 200) {
					if (this.userInfo._id == item._id) {
						uni.setStorageSync('userInfo', item)
					}
					this.getUserList()
				}
				
			}
		},
	};
</script>

<style scoped>
	.list {
		width: 100%;
		height: 100%;
		padding: 5px;
		box-sizing: border-box;
	}

	.list li {
		width: 100%;
		margin-bottom: 10px;
		padding: 10px;
		box-sizing: border-box;
		box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.4);
		border-radius: 5px;
	}

	.list li .header {
		width: 100%;
		height: 40px;
		display: flex;
		align-items: center;
		border-bottom: 1px solid #999;
	}

	.list li .header image {
		width: 30px;
		height: 30px;
		border-radius: 5px;
		box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.4);
		margin-right: 8px;
	}

	.list li .header p {
		font-size: 12px;
		font-weight: 700;
	}

	.vanRadioGroup {
		display: flex;
		align-items: center;
	}
</style>